﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NanoERP.Infrastructure;
using LL.Common;

namespace NanoERP.Modules.SystemMgmt
{
    [Module("FrmMgrUser")]
    public partial class FrmUserList : ModuleForm
    {
        public FrmUserList()
        {
            InitializeComponent();
        }


    }


    public class ListCorePresenter<T>
        where T: class,IIdentity<Guid>
    {
        #region Override

        //protected override void InitControlEvents()
        //{
        //    base.InitControlEvents();
        //    var grid = GetGrid();
        //    grid.PageIndexChange += OnPageIndexChange;
        //    grid.Sort += OnSort;
        //    grid.RowCommand += OnRowCommand;
        //}

        //protected override void OnLoad(EventArgs e)
        //{
        //    base.OnLoad(e);
        //    if (!IsPostBack)
        //        OnFirstLoad();
        //    //处理回发数据(ReceivedValues)
        //    if (!RecivedValues["_Cmd"].IsNullOrEmpty())
        //        ProcessCallBackCmd();
        //}

        //protected override PageType GetPageType()
        //{
        //    return PageType.ListPage;
        //}

        //protected override void InitToolbar()
        //{
        //    base.InitToolbar();
        //    InitGridCommands();
        //}

        //#endregion

        //#region HandleEvents

        ///// <summary>
        ///// 处理分页事件
        ///// </summary>
        //protected virtual void OnPageIndexChange(object sender, GridPageEventArgs e)
        //{
        //    LoadDataRequest req = BuildRequest();
        //    req.PagingInfo.PageIndex = e.NewPageIndex;
        //    DoLoadData(req);
        //}

        ///// <summary>
        ///// 处理排序事件
        ///// </summary>
        //protected virtual void OnSort(object sender, GridSortEventArgs e)
        //{
        //    var grid = GetGrid();
        //    grid.SortDirection = e.SortDirection;
        //    grid.SortColumnIndex = e.ColumnIndex;
        //    RefreashLoad();
        //}

        ///// <summary>
        ///// 处理行事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //protected virtual void OnRowCommand(object sender, GridCommandEventArgs e)
        //{
        //    ProcessRowCommand(e.CommandName, e.CommandArgument, e.RowIndex);
        //}

        ///// <summary>
        ///// 处理行事件
        ///// </summary>
        ///// <param name="commandName"></param>
        ///// <param name="commandArgument"></param>
        ///// <param name="rowIndex"></param>
        //protected virtual void ProcessRowCommand(string commandName, string commandArgument, int rowIndex)
        //{
        //    if (Rights.IsNullOrEmpty())
        //        throw new NotImplementedException(string.Format("遇到不能处理的RowCommand：{0}", commandName));
        //    SYS_Right cmd = Rights.FirstOrDefault(d => d.Code == commandName);
        //    if (cmd == null)
        //        throw new NotImplementedException(string.Format("遇到不能处理的RowCommand：{0}", commandName));
        //    FireCommandEvent(cmd);
        //}

        //#endregion

        //#region TemplateMethods

        //protected virtual void OnFirstLoad()
        //{
        //    FirstLoad();
        //}

        ///// <summary>
        ///// 构建查询条件
        ///// </summary>
        ///// <param name="queryConditions"></param>
        //protected virtual void BuildQuery(QueryConditions queryConditions)
        //{

        //}

        ///// <summary>
        ///// 处理数据列表工具栏命令
        ///// </summary>
        ///// <param name="selectedIds">Grid选中的数据</param>
        ///// <returns></returns>
        //protected virtual bool ProcessCustomCommand(SYS_Right command, string[] selectedIds)
        //{
        //    throw new NotImplementedException(
        //        string.Format("遇到不能处理的Command:{0}（{1}）,请重写ProcessCustomCommand方法",
        //                    command.Command, command.Name));
        //}

        ///// <summary>
        ///// 处理回发命令
        ///// </summary>
        //protected virtual void ProcessCallBackCmd()
        //{
        //    throw new NotImplementedException("请重写ProcessCallBackCmd方法");
        //}

        //#endregion

        //#region HelpMethods

        ///// <summary>
        ///// 注册Grid事件
        ///// </summary>
        //protected virtual void InitGridCommands()
        //{
        //    if (Rights.IsEmpty() || GetPageType() == PageType.FormPage)
        //        return;

        //    foreach (var command in Rights)
        //    {
        //        var flag = command.CommandFlags.Cast<CommandFlags>();
        //        SYS_Right command1 = command;
        //        if (flag.IsDefined(CommandFlags.AsGrid2Event))
        //        {
        //            var grid = GetGrid();
        //            grid.EnableRowDoubleClickEvent = true;
        //            grid.RowDoubleClick += (x, y) => FireCommandEvent(command1);
        //        }
        //    }
        //}

        /// <summary>
        /// 首次/重新加载数据
        /// </summary>
        protected virtual void FirstLoad()
        {
            //LoadDataRequest request = BuildRequest();
            //DoLoadData(request);
        }

        /// <summary>
        /// 刷新当前页数据
        /// </summary>
        /// <param name="clearSelection">是否清除勾选状态</param>
        protected virtual void RefreashLoad(bool clearSelection = false)
        {
            //LoadDataRequest request = BuildRequest(false);
            ////获取选中行DataKeys
            //var keys = GetGrid().GetSelectedDataKeys();
            ////重新加载数据
            //DoLoadData(request);
            ////还原选中行
            //if (!clearSelection)
            //    GetGrid().SelectRowsByDataKeys(keys);
        }

        ///// <summary>
        ///// 加载列表数据
        ///// </summary>
        ///// <param name="request"></param>
        //protected virtual void DoLoadData(LoadDataRequest request)
        //{
        //    var response = LoadData(request);
        //    BindListToGrid(response);
        //}

        ///// <summary>
        ///// 根据LoadDataRequest加载数据
        ///// </summary>
        //protected abstract LoadDataResponse LoadData(LoadDataRequest request);

        ///// <summary>
        ///// 根据查询结果绑定数据到列表
        ///// </summary>
        //protected virtual void BindListToGrid(LoadDataResponse response)
        //{
        //    var grid = GetGrid();
        //    if (response.Request.PagingInfo != null)
        //    {
        //        grid.PageIndex = response.Request.PagingInfo.PageIndex;
        //        grid.PageSize = response.Request.PagingInfo.PageSize;
        //    }
        //    grid.RecordCount = response.TotalRows;
        //    grid.DataSource = response.Data;
        //    grid.DataBind();
        //}

        ///// <summary>
        ///// 构建一个查询请求并返回
        ///// </summary>
        //protected virtual LoadDataRequest BuildRequest(bool newRequest = true)
        //{
        //    var request = new LoadDataRequest();
        //    var grid = GetGrid();
        //    //从界面表格获取分页排序信息
        //    if (IsDatabasePaging)
        //    {
        //        request.PagingInfo = grid.GetPaggingInfo();
        //        //新的查询应该从第一页开始
        //        if (newRequest)
        //            request.PagingInfo.PageIndex = 0;
        //    }
        //    //从界面获取排序信息
        //    request.Order = grid.GetOrder();
        //    //由子页面添加自定义的条件
        //    BuildQuery(request.QueryCondition);
        //    return request;
        //}

        ///// <summary>
        ///// 获取当前页面的Grid控件
        ///// </summary>
        //protected virtual DataGridView GetGrid()
        //{
        //    throw new NotImplementedException();
        //}

        ///// <summary>
        ///// 获取Grid所有选中行Id
        ///// </summary>
        //protected override string[] GetSelectedData(SYS_Right command)
        //{
        //    //DataGridView grid = GetGrid();
        //    //if (grid.DataKeyNames == null || grid.DataKeyNames.Length < 1)
        //    //    throw new InvalidOperationException("请为Grid控件设置DataKeyNames 或 重写GetSelectedData()方法");
        //    //return grid.SelectedRowIndexArray.Select(
        //    //    rowIndex => grid.DataKeys[rowIndex][0].ToDefaultString(null)).ToArray();
        //    throw new NotImplementedException();
        //}

        #endregion

        #region PreparedEvents

        /// <summary>
        /// 预定义的刷新当前页事件处理程序
        /// </summary>
        /// <remarks>用于设置页面控件事件</remarks>
        protected virtual void RefreashLoad(object sender, EventArgs e)
        {
            //刷新当前页数据
            RefreashLoad();
        }

        /// <summary>
        /// 预定义的重新加载数据事件处理程序
        /// </summary>
        /// <remarks>用于设置页面控件事件</remarks>
        protected virtual void FirstLoad(object sender, EventArgs e)
        {
            //重新加载数据
            FirstLoad();
        }

        #endregion


        //=-----
        public void InitControlEvents()
        {

        }

        public void InitControls()
        {

        }

        public void LoadData()
        {

        }

        public IList<Guid> GetSelectedKeys()
        {
            throw new NotImplementedException();
        }
         

        public void RefreshLoad()
        {

        }

        public void ChangePageIndex(int newPage)
        {

        }


        public bool IsDatabasePaging { get; set; }
    }

}
